#Please cite as: Lerchenmueller MJ. Gender Designation via Genderize API with Unlimited Request. doi:10.6084/m9.figshare.4563814

#Install genderize package by Kamil Wais (https://github.com/kalimu/genderizeR ) 
install.packages('genderizeR')
suppressPackageStartupMessages(library(genderizeR))

#Set up working directory for genderize query
setwd("C:/Users/Marc/Rwork")
getwd()
#Check if the input datafile one would like to use is located in current directory
dir()
source("textPrepare.R")

#Read input file (vector of names) one wants to query from genderize and transfer into character vector in R
#Note: read.csv always assumes header to be true so you have to explicitly set it to false 
#File example below queries 289170 names in one go
dat <- read.csv("Unique_Forenames_289170.csv", header=F)$V1
as.character(dat)
m <- split(dat, ceiling(seq_along(dat)/10))

#Loop over batches of names as genderize API only allows query of 10 names at a time - even with paid for API key (as of Jan 2017)
#Note: It is an option to also loop over textPrepare from the genderizeR package but since names are excluded after preparation a receiving data frame is harder to define 
#The below inserted API key is expired. For the code to work, one needs to update the API key (payment on genderize homepage required)
df <- data.frame(matrix(ncol = 4, nrow = length(dat))) 
d <- split(df,rep(1:max(seq_along(m)), each = 10))
for(i in seq_along(m)) {
	d[[i]] <- findGivenNames(m[[i]], apikey = "720be45709488c2940c776c8b52b8829", queryLength = 10, progress = T, ssl.verifypeer = T)
	write.table(d[[i]],file ="output.csv", append = T, col.names = F)  
}

# END of Code
